package com.thirteengroup.casemg.controller;



import com.fasterxml.jackson.databind.ObjectMapper;
import com.thirteengroup.casemg.domain.JasonResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//import java.net.http.HttpResponse;

@ControllerAdvice
public class SSMExceptionHandler {

    @ExceptionHandler
    public void handlerException(Exception ex, HandlerMethod method, HttpServletResponse response){
        String page;
        //打印出异常
        try {
            ex.printStackTrace();
            JasonResult jasonResult=new JasonResult("出异常了！",false);
            response.setContentType("applicatin/json;charset=utf-8");
            ObjectMapper mapper=new ObjectMapper();
            //转换为jsaon字符串
            String jsonstr=mapper.writeValueAsString(jasonResult);
            response.getWriter().print(jsonstr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
